<?php

if(isset($_SESSION['player'])){
	header('Location: index.php?p=home');	
}

if(isset($_POST['name'], $_POST['email'], $_POST['localisation'])){
	
	try {
		$_player = new Player();
		
		if(	$_player->isValidName($_POST['name']) &&
			$_player->isValidEmail($_POST['email']) &&
			Localisation::isValidLocale($_POST['localisation'])){
			
			$password = $_player->generatePassword();	
			
			$_player->setName($_POST['name']);
			$_player->setEmail($_POST['email']);
			$_player->setLocalisation($_POST['localisation']);
			$_player->setPassword($password);
			
			$_SESSION['localisation'] = $_player->getLocalisation();
			
			/*
			sendEmail(
				$_player->getEmail(),
				TITLE,
				sprintf(
					"Bonjour %s,\n\r".
					"Bienvenue dans l'univers des Seelies.\n\r".
					"Votre mot de passe est : %s\n\r".
					"Vous pourrez le changer une fois connecté ".
					" à votre espace.\n\r".
					"À bientôt sur %s.",
					$_player->getName(),
					$password,
					APP
				)
			);
			*/
			
			$_player->save();
		}
		
		header('Location: index.php?p=home');
	}
	catch(Exception $e){
		
		Log::addException($e);
		header('Location: index.php?p=home');
	}
	
}

?>